//
//  YHToolClass.swift
//  FKTbroker
//
//  Created by daojial on 2019/11/26.
//  Copyright © 2019 RRJ. All rights reserved.
//

import UIKit

class YHToolClass: NSObject {

/**计算 文本高度 */
    class func getHeightWithText(origStr:String,fontSize:CGFloat,maxWidth:CGFloat) -> CGFloat{
      
        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineBreakMode = .byCharWrapping
        
        let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize),NSAttributedString.Key.paragraphStyle:paragraphStyle]
        let textRect = CGSize.init(width: maxWidth, height: CGFloat(MAXFLOAT))
        
        let textHeight = origStr.boundingRect(with: textRect, options: [.usesFontLeading,.usesLineFragmentOrigin], attributes: attributes, context: nil).size.height
        
        return textHeight
    }
/**计算一行文本的Size*/
  class func getTextSize(origStr:String,fontSize:CGFloat,maxWith:CGFloat) -> CGSize {
        
        let returnSize = origStr.boundingRect(with: CGSize.init(width: maxWith, height: CGFloat(MAXFLOAT)), options: [.usesFontLeading,.usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize)], context: nil).size
        
        return returnSize
    }
    
    
/**设置特殊字的颜色字号*/
class func setNSMutabString(origStr:String , spelStr:String , splColor:UIColor , splFontSize:CGFloat) -> NSMutableAttributedString {
        let mustring = NSMutableAttributedString.init(string: origStr)
        if spelStr.isEmpty == true {
        return mustring;
        }else{

        let spelStrRange = NSRange.init(location: findIdx(origStr: origStr, splStr: spelStr), length: spelStr.count)

        mustring.addAttribute(NSAttributedString.Key.foregroundColor, value: splColor, range: spelStrRange)

        if splFontSize != 0
        {
        mustring.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: splFontSize), range: spelStrRange)
        }


        return mustring
        }
    }
 
    
/**查找位置*/
    
  class  func findIdx(origStr:String,splStr:String) -> Int {
        let range = origStr.range(of: splStr)!
        let end_idx = origStr.distance(from: origStr.startIndex, to: range.lowerBound)
        
        return end_idx;
    }
    
/**富文本末尾加图片
 *origStr:文字
 *imageName:图片名
 *imageReact:React
 *insertPosition：插入位置
 */
class func stringInsertImage(origStr:String,imageName:String,imageReact:CGRect,insertPosition:Int) -> NSAttributedString {
    let attributedStr = NSMutableAttributedString.init(string: origStr)
    //创建文本附件图片 插入到富文本中
    let attchImage = NSTextAttachment.init()
    attchImage.image = UIImage.init(named: imageName)
    attchImage.bounds = imageReact
    let stringImage = NSAttributedString.init(attachment: attchImage)
    attributedStr.insert(stringImage, at: insertPosition)

    return attributedStr
}

    
    
    
}
